package com.palm.hubo2o.util;

import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.ViewGroup;

import com.palm.hubo2o.R;
import com.palm.hubo2o.model.Bet;
import com.palm.palmutils.widget.BallView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dest1 on 2015/10/22.
 */
public class BallUtils {
    //    private static String[] EMPTY_ARRAYS = new String[0];
    private final static int red = 0xffff4444;
    private final static int blue = 0xff33b5e5;

    /**
     * 解析双色球开奖号码（7位二位数字+,序列）
     *
     * @param balls 未序列化SSQ开奖号
     * @return 序列化开奖号数组
     */
    public static String[] analyzeSsqBalls(String balls) {
        if (TextUtils.isEmpty(balls))
            return createEmptyBalls(7);
        String[] array = balls.split(",", -1);
        if (array.length == 7)
            return array;
        return createEmptyBalls(7);
    }

    /**
     * 解析3D开奖号码（3位一位数字+|序列）
     *
     * @param balls 未序列化3D开奖号
     * @return 序列化开奖号数组
     */
    public static String[] analyzeD3Balls(String balls) {
        if (TextUtils.isEmpty(balls))
            return createEmptyBalls(3);
        String[] array = balls.split("\\|", -1);
        if (array.length == 3)
            return array;
        return createEmptyBalls(3);
    }

    /**
     * 解析快3开奖号码（3位二位数字+,序列）
     *
     * @param balls 未序列化K3开奖号
     * @return 序列化开奖号数组
     */
    public static String[] analyzeK3Balls(String balls) {
        if (TextUtils.isEmpty(balls))
            return createEmptyBalls(3);
        String[] array = balls.split(",", -1);
        if (array.length == 3)
            return array;
        return createEmptyBalls(3);
    }

    /**
     * 解析七乐彩开奖号码（8位二位数字+,序列）
     *
     * @param balls 未序列化QLC开奖号
     * @return 序列化开奖号数组
     */
    public static String[] analyzeQlcBalls(String balls) {
        if (TextUtils.isEmpty(balls))
            return createEmptyBalls(8);
        String[] array = balls.split(",", -1);
        if (array.length == 8)
            return array;
        return createEmptyBalls(8);
    }

    /**
     * 解析22选5开奖号码（8位二位数字+,序列）
     *
     * @param balls 未序列化225开奖号
     * @return 序列化开奖号数组
     */
    public static String[] analyze522Balls(String balls) {
        if (TextUtils.isEmpty(balls))
            return createEmptyBalls(5);
        String[] array = balls.split(",", -1);
        if (array.length == 5)
            return array;
        return createEmptyBalls(5);
    }

    public static String[] analyzeBalls(String balls, EnumCode.gameId gameId) {
        String[] array;
        switch (gameId) {
            case SSQ:
                array = analyzeSsqBalls(balls);
                break;
            case D3:
                array = analyzeD3Balls(balls);
                break;
            case HBP3:
                array = analyzeK3Balls(balls);
                break;
            case QLC:
                array = analyzeQlcBalls(balls);
                break;
//            case C522:
//                array = analyze522Balls(balls);
//                break;
            default:
                array = createEmptyBalls(0);
        }
        return array;
    }


    /**
     * 添加并初始化球池布局
     *
     * @param group 球池布局view
     * @param reds  红球组
     * @param blues 篮球组
     */
    public static void addBallGroups(ViewGroup group, List<String> reds, List<String> blues) {
        if (reds == null)
            return;
        for (int i = 0; i < reds.size(); i++) {
            BallView ballView = new BallView(group.getContext());
            ballView.setNumber(reds.get(i));
            ballView.setChecked(true);
            ballView.setBallColor(ContextCompat.getColor(group.getContext(), R.color.red));
            group.addView(ballView);
        }
        if (blues == null)
            return;
        for (int i = 0; i < blues.size(); i++) {
            BallView ballView = new BallView(group.getContext());
            ballView.setNumber(reds.get(i));
            ballView.setChecked(true);
            ballView.setBallColor(ContextCompat.getColor(group.getContext(), R.color.blue));
            group.addView(ballView);
        }
    }


    public static List<BallView> createBallGroup(ViewGroup group, EnumCode.gameId gameId) {
        return createBallGroup(group, gameId, false);
    }

    /**
     * 添加球池布局
     *
     * @param group   球池布局view
     * @param gameId  彩种
     * @param notBall 是否赋予球形背景
     * @return
     */
    public static List<BallView> createBallGroup(ViewGroup group, EnumCode.gameId gameId, boolean notBall) {
        List<BallView> list = new ArrayList<>();
        String[] empty = analyzeBalls("", gameId);
        for (int i = 0; i < empty.length; i++) {
            BallView ballView = new BallView(group.getContext());
//            ballView.setLayoutParams(new ViewGroup.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT));
            ballView.setChecked(true);
            if (i == empty.length - 1 && (gameId == EnumCode.gameId.SSQ ||
                    gameId == EnumCode.gameId.QLC)) {
                ballView.setBallColor(ContextCompat.getColor(group.getContext(), R.color.blue));
            } else {
                ballView.setBallColor(ContextCompat.getColor(group.getContext(), R.color.red));
            }
            ballView.setAwarded(!notBall);
            group.addView(ballView);
            list.add(ballView);
        }
        return list;
    }

    private static String[] createEmptyBalls(int size) {
        return new String[size];
    }

//    public static String createBallTag(EnumCode.gameId gameId)

    /**
     * 机选
     *
     * @param ballNum 生成的球数
     * @param Range   范围(1-100)
     ***/
    public static Integer[] getMachineBall(int ballNum, int Range) {

        int count = 0;
        List<Integer> ballList = new ArrayList<>();
        Map<Integer, Integer> ballMap = new HashMap<Integer, Integer>();
        while (count != ballNum) {
            int ball = (int) (Math.random() * Range);
            if (ballMap.get(ball) == null && ball != 0) {
                ballMap.put(ball, ball);
                ballList.add(ball);
                count++;
            }
        }
        return ballList.toArray(new Integer[ballList.size()]);
    }

    /*
    * 返回带0的数
    * **/
    public static String getStrNum(int num) {
        return num >= 10 ? num + "" : "0" + num;
    }


    /**
     * 机选返回一注随机单式
     *
     * @param gameId
     * @return
     */
//    public static Bet getMachineBall(EnumCode.gameId gameId) {
//        StringBuilder sb = new StringBuilder();
//        Bet temp = new Bet(gameId);
//        temp.note = 1;
//        switch (gameId) {
//            case SSQ:
//                temp.serial = Constant.SSQ_PLAY_TYPE_DS;
//                int[] red = MathUtils.randomIn(33, 6);
//                Arrays.sort(red);
//                for (int i : red) {
//                    sb.append(String.format("%02d,", ++i));
//                }
//                sb.replace(sb.length() - 1, sb.length(), ":");
//                sb.append(String.format("%02d", MathUtils.random(16) + 1));
//                temp.balls = new String[]{sb.toString()};
//                break;
//            case QLC:
//                temp.serial = Constant.QLC_PLAY_TYPE_DS;
//                int[] qlc = MathUtils.randomIn(30, 7);
//                Arrays.sort(qlc);
//                for (int i : qlc) {
//                    sb.append(String.format("%02d,", ++i));
//                }
//                sb.deleteCharAt(sb.length() - 1);
//                temp.balls = new String[]{sb.toString()};
//                break;
//            case HBP3:
//                temp.serial = Constant.K3_PLAY_TYPE_HZ_DS;
//                temp.balls = new String[]{MathUtils.random(K3SumFragment.awards.length) + 4 + ""};
//                break;
//            case D3:
//                temp.serial = Constant.D3_PLAY_TYPE_DS;
//                temp.balls = new String[]{MathUtils.random(10) + ":" + MathUtils.random(10) + ":" + MathUtils.random(10)};
//                break;
////            case C522:
////                temp.serial = Constant.C225_PLAY_TYPE_DS;
////                int[] c522 = MathUtils.randomIn(22, 5);
////                Arrays.sort(c522);
////                for (int i : c522) {
////                    sb.append(String.format("%02d,", ++i));
////                }
////                sb.deleteCharAt(sb.length() - 1);
////                temp.balls = new String[]{sb.toString()};
////                break;
//            default:
//                break;
//        }
//        return temp;
//    }

    /**
     * 订单列表重新解析上色
     */
    public static CharSequence coloredOrder(Bet o) {
        StringBuilder sb = new StringBuilder();
        SpannableStringBuilder builder = new SpannableStringBuilder();
        for (String red : o.getReds()) {
            sb.append(red).append(" ");
        }
//        sb.append(" ");
        for (String tuo : o.getTuos()) {
            sb.append(tuo).append(" ");
        }

        if (sb.length() != 0) {
            SpannableString s = new SpannableString(sb.toString());
            s.setSpan(new ForegroundColorSpan(red), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            builder.append(s).append(" ");
        }
        sb.setLength(0);
        for (String blue : o.getBlues()) {
            sb.append(blue).append(" ");
        }

        if (sb.length() != 0) {
            SpannableString s = new SpannableString(sb.toString());
            s.setSpan(new ForegroundColorSpan(blue), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            builder.append(s);
        }

        return builder;
    }
}


